﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.UI;
using System.Web.Mvc;
using System.Text;
using System.Web.Routing;

namespace MvcCarConsume.WebUI
{
    public static class PicShowHelper
    {

        private static string DefaultPicPath = "/Content/Images/CarPic/default.jpg";
        private static string DefaultDir = "/Content/Images";

        public static string PicShow(this HtmlHelper html,int id)
        {
            using (var db = new Domain.Entites.MvcCarDBEntities())
            {
                var car = db.Car.FirstOrDefault(c => c.CarId == id);
                if (car == null)
                    return DefaultPicPath;
                else
                {
                    if (car.ImagePath == null)
                        return DefaultPicPath;
                    string Path = DefaultDir + car.ImagePath.Replace('\\', '/') + "index.jpg";
                    if (File.Exists(AppDomain.CurrentDomain.BaseDirectory + Path.Remove(0, 1)))
                        return Path;
                    else
                        return DefaultPicPath;

                    { }
                }
            }
        }

        public static string PicShow(this HtmlHelper html)
        {
            return DefaultPicPath;
        }
    }
}